package com.dz.Incubator.service.impl;

import com.dz.Incubator.config.Constant;
import com.dz.Incubator.config.CustomException;
import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.dao.NewsDao;
import com.dz.Incubator.entity.NewsEntity;
import com.dz.Incubator.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

@Service
public class NewsServiceImpl implements NewsService {

    private static final Logger logger = LoggerFactory.getLogger(NewsService.class);

    @Autowired
    private NewsDao newsDao;

    @Override
    public NewsEntity insert(NewsEntity newsEntity) {
        if (null == newsEntity) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        newsEntity.setCreateTime(new Date());
        newsEntity.setUpdateTime(new Date());
        newsEntity.setDelTag(Constant.PO_ENTITY_DELTAG_NO_TAG);
        int ret = newsDao.insert(newsEntity);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "添加失败");
        }
        return newsEntity;
    }

    @Override
    public NewsEntity update(NewsEntity newsEntity) {
        if (null == newsEntity) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        newsEntity.setUpdateTime(new Date());
        int ret = newsDao.update(newsEntity);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "修改失败");
        }

        return newsEntity;
    }

    @Override
    public Long delete(Long id) {
        if (null == id) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        int ret = newsDao.delete(id);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "删除失败");
        }
        return id;
    }

    @Override
    public List<NewsEntity> find(Long[] ids) {
        if (null == ids || ids.length <= 0 ) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        List<NewsEntity> datas = newsDao.find(ids);
        return datas;
    }

    @Override
    public NewsEntity get(Long id) {
        if (null == id) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        NewsEntity newsEntity = newsDao.get(id);
        return newsEntity;
    }

    @Override
    public List<NewsEntity> findAll() {
        List<NewsEntity> datas = newsDao.findAll();
        return datas;
    }

    @Override
    public Pagination  findByPagination(Pagination pagination) {
        if (null == pagination) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        List<NewsEntity> datas = newsDao.findByPagination(pagination);
        pagination.setList(datas);
        return pagination;
    }

    @Override
    public List<NewsEntity> insertBatch(List<NewsEntity> list) {
        if (null == list || list.size() <= 0) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        for (NewsEntity entity : list) {
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
        }
        int ret = newsDao.insertBatch(list);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "批量添加失败");
        }
        return list;
    }

    @Override
    public List<NewsEntity> updateBatch(List<NewsEntity> list) {
        if (null == list || list.size() <= 0) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        int ret = newsDao.updateBatch(list);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "批修改失败");
        }
        return list;
    }

    @Override
    public List<Long> deleteBatch(Long[] ids) {
        if (null == ids || ids.length <= 0) {
            throw new CustomException(ReturnCode.CHECK_ERROR.getCode(), ReturnCode.CHECK_ERROR.getMsg());
        }
        int ret = newsDao.deleteBatch(ids);
        if (ret <= 0) {
            throw new CustomException(ReturnCode.BUSINESS_ERROR.getCode(), "批删除失败");
        }
        return Arrays.asList(ids);
    }
}